package com.sino.androiddesign.design

import android.annotation.SuppressLint
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.BaseTransientBottomBar
import android.support.design.widget.Snackbar
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.activity_other.*

/**
 * textEditInputLayout
 * floatAction
 * SnackBar
 */
class OtherActivity : AppCompatActivity() {

    @SuppressLint("ResourceAsColor")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_other)


        other_pass_tl.editText!!.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                var length = s!!.length
                if (length > other_pass_tl.counterMaxLength) {
                    other_pass_tl.error = "密码长度不能超过20个字符"
                }
            }
        })

        /**
         * floatActionButton 就是一个普通的Button
         */
        toolbar_fab.setOnClickListener {
            // 父布局不是 CoordinatorLayout
            var snackbar = Snackbar.make(toolbar_fab, "我是Snackbar", Snackbar.LENGTH_LONG)
            snackbar.setAction("撤销") { Toast.makeText(this@OtherActivity, "提示信息", Toast.LENGTH_SHORT).show() }

            snackbar.setActionTextColor(android.R.color.holo_green_light)
//            snackbar.setText("设置文本")
            snackbar.show()
        }

        /**
         * SnackBar 会把值存储在view中,并在view向上寻找直到父布局为CoordinatorLayout,
         * 或者FramLayout  会出现在他们的底部
         */
        toolbar_normal_btn.setOnClickListener {
            Snackbar.make(toolbar_normal_btn, "一个普通的SnackBar", Snackbar.LENGTH_LONG).show()
        }

        toolbar_special_btn.setOnClickListener {

            var firstTime: Long = 0
            var snackbar = Snackbar.make(toolbar_special_btn, "带回调函数", Snackbar.LENGTH_LONG)
            snackbar.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
                //出现调用
                override fun onShown(transientBottomBar: Snackbar?) {
                    super.onShown(transientBottomBar)
                    firstTime = System.currentTimeMillis()
                }

                //消失的时候调用
                override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
                    super.onDismissed(transientBottomBar, event)
                    var second: Long = System.currentTimeMillis()
                    println("持续时间 ==${second - firstTime}  ")
                }
            })
            snackbar.setAction("出现吧") {
                Toast.makeText(this@OtherActivity, "123", Toast.LENGTH_SHORT).show()
            }
            snackbar.setActionTextColor(Color.RED)
            snackbar.show()
        }

    }
}
